import { RouteRecordRaw, useRoute } from "vue-router";
import router from "@/router/index";
import store from "@/store/index";
export const initMenus = async () => {
    await store.dispatch("getMenu");
    const secondMenus = store.state.menus.map((item: any) => item.children).reduce((pre, item, index) => [...pre, ...item], []);
    const mapedMenus = secondMenus.map((item: any) => {
        const path = item.url;
        const pathArray = path.split("/");
        const name = pathArray[pathArray.length - 1];
        const routeItem = {
            path,
            name,
            component: () => import(`@/views/${name}/${name}`),
        };
        return routeItem;
    });
    router.addRoute({
        path: "/admin",
        name: "admin",
        component: () => import("@/layouts/admin.vue"),
        redirect: "/admin/dashboard",
        children: [
            {
                path: "/admin/dashboard",
                name: "dashboard",
                component: () => import("@/views/dashboard/dashboard.vue"),
            },
            ...mapedMenus,
            {
                path: "/:pathMathch(.*)",
                name: "404",
                redirect: "/404",
            },
        ],
    });
    return true;
};
